import pandas as pd

df_model1 = pd.read_csv('pred-csv-fu/tf_efficientnet_b4_ns/k10-vot-flip.csv')
df_model2 = pd.read_csv('pred-csv-fu/resnest50d/k10-vot-flip.csv')
df_model3 = pd.read_csv('pred-csv-fu/tf_efficientnetv2_s_in21ft1k/k10-vot-flip.csv')
df_model1 = df_model1.sort_values(by='image_id', axis=0)
df_model2 = df_model2.sort_values(by='image_id', axis=0)
df_model3 = df_model3.sort_values(by='image_id', axis=0)
df_model1.index = range(df_model1.shape[0])
df_model2.index = range(df_model2.shape[0])
df_model3.index = range(df_model3.shape[0])
print(df_model1.tail())
print(df_model2.tail())
print(df_model3.tail())

df_all = df_model1.copy()
df_all.rename(columns={'category_id': 'label_model1'}, inplace=True)
df_all['label_model2'] = df_model2.copy()['category_id']
df_all['label_model3'] = df_model3.copy()['category_id']
print(df_all.head())

df_all['category_id'] = 0
for rows in range(len(df_all)):
    # model1 == model2 or model3  model1是众数
    if (df_all['label_model1'].iloc[rows] == df_all['label_model2'].iloc[rows]) or (
            df_all['label_model1'].iloc[rows] == df_all['label_model3'].iloc[rows]):
        df_all['category_id'].iloc[rows] = df_all.copy()['label_model1'].iloc[rows]
    # model2 == model3  model2是众数
    elif df_all['label_model2'].iloc[rows] == df_all['label_model3'].iloc[rows]:
        df_all['category_id'].iloc[rows] = df_all.copy()['label_model2'].iloc[rows]
    # 三者都不同，2分类不会出现此情况
    else:
        print('绝不会出现此打印！')
        df_all['category_id'].iloc[rows] = df_all.copy()['label_model1'].iloc[rows]
print(df_all.head())

df_final = df_all.copy()[['image_id', 'category_id']]
print(df_final.head())

df_final.to_csv('pred-csv/3model-voting-A.csv', index=False)
print('Final results successfully saved!')
